import requests, sys
from bs4 import BeautifulSoup

"""
类说明:下载《笔趣看》网小说《一念永恒》
Parameters:
    无
Returns:
    无
"""


class download:
    def __init__(self, server, target):
        self.server = server
        self.target = target
        self.names = []
        self.urls = []
        self.nums = 0

    """
       函数说明:获取下载链接
    Parameters:
        无
    Returns:
        无
    """

    def get_download_url(self):
        req = requests.get(url=self.target)
        req.encoding = req.apparent_encoding
        html = req.text
        div_bf = BeautifulSoup(html, features="html.parser")
        div = div_bf.find_all("div", class_="listmain")
        a_bf = BeautifulSoup(str(div[0]), features="html.parser")
        a_list = a_bf.find_all('a')
        self.nums = len(a_list[13:])
        for each in a_list[13:]:
            self.names.append(each.string)
            self.urls.append(self.server + each.get("href"))

    """
        函数说明:获取章节内容
    Parameters:
        target - 下载连接(string)
    Returns:
        texts - 章节内容(string)
    """

    def get_contents(self, target):
        req = requests.get(url=target)
        req.encoding = req.apparent_encoding
        html = req.text
        bf = BeautifulSoup(html, "html.parser")
        texts = bf.find_all("div", class_="showtxt")
        texts = texts[0].text.replace("\xa0" * 8, "\n\n")
        return texts

    """
        函数说明:将爬取的文章内容写入文件
    Parameters:
        name - 章节名称(string)
        path - 当前路径下,小说保存名称(string)
        text - 章节内容(string)
    Returns:
        无
    """

    def writer(self, name, path, text):
        write_flag = True
        with open(path, 'a', encoding="utf-8") as file:
            file.write(name + "\n")
            file.writelines(text)
            file.write("\n\n")


if __name__ == "__main__":
    dl = download(server="https://www.biqukan8.cc", target="https://www.biqukan8.cc/1_1094/")
    dl.get_download_url()
    print("<<一念永恒>>开始下载")
    path = "C:/Users/e-Xingfu.Yang/Desktop/Tmp/spider、一念永恒.txt"
    for i in range(dl.nums):
        dl.writer(dl.names[i], path, dl.get_contents(dl.urls[i]))
        sys.stdout.write("已下载: %.3f%%" % float(i / dl.nums) + "\r")
        sys.stdout.flush()
    print("<<一念永恒>>下载完成")
